package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.GuiderHistoryRequestContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.ShoppersHistoricalBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.GuiderHistoryRequestActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/27.
 */

public class GuiderHistoryRequestPresenter implements GuiderHistoryRequestContract.Presenter {

    private GuiderHistoryRequestContract.View mView;
    private GuiderHistoryRequestActivity mContext;

    public GuiderHistoryRequestPresenter(GuiderHistoryRequestActivity activity,
                                         GuiderHistoryRequestActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getGuiderHistory(int pageIndex, int pageSize) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");

        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(mContext).getMember_id());
        map.put("member_type_key", "consumer");

        MineApiDataSource.getInstance()
                .getGuiderHistory(InfoProvider.getUserToken(mContext).getUser_token(),
                        "saleAssistant.member.saleAssistantHistoryPageFind",
                        new Gson().toJson(maps), new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<ShoppersHistoricalBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<ShoppersHistoricalBean> response) {
                        List<ShoppersHistoricalBean.Convient> guiderHistoryList =
                                response.data.getList();

                        if (guiderHistoryList.size() > 0) {
                            // 显示数据
                            mView.getGuiderHistorySucc(guiderHistoryList);
                        } else {
                            // 没有数据
                            if (pageIndex == 1) mView.showEmpty();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
